/****************************************************************************
 * Copyright (c) 2021 Qualcomm Technologies International, Ltd.
****************************************************************************/
/**
 * \defgroup wind_noise_detect
 * \ingroup capabilities
 * \file  wind_noise_detect.h
 *
 * Wind Noise Detect (WND) operator public header file.
 *
 */

#ifndef _WIND_NOISE_DETECT_H_
#define _WIND_NOISE_DETECT_H_

/******************************************************************************
Include Files
*/

#include "capabilities.h"

#include "mem_utils/memory_table.h"     /* Memory table functions */

#include "base_aud_cur_op.h"            /* Class functions */
#include "aanc_security_public.h"       /* AANC security library */
#include "wnd200_public.h"              /* WND private library */
#include "wind_noise_detect_gen_c.h"    /* Autogenerated WND definitions */
#include "wind_noise_detect_defs.h"     /* Shared WND definitions */
#include "wind_noise_detect_event.h"    /* WND event definitions */

/****************************************************************************
Public Variable Definitions
*/

extern const CAPABILITY_DATA wind_noise_detect_cap_data; /* Capability data */

/******************************************************************************
Private Constant Definitions
*/
#define WND_STATISTICS               WIND_NOISE_DETECT_STATISTICS
#define WND_PARAMETERS               WIND_NOISE_DETECT_PARAMETERS
#define WND_VERSION_MAJOR            WIND_NOISE_DETECT_WIND_NOISE_DETECT_VERSION_MAJOR
#define WND_GetDefaults              WIND_NOISE_DETECT_GetDefaults

/* Number of statistics reported by the capability */
#define WND_N_STAT                   (sizeof(WND_STATISTICS)/sizeof(ParamType))

/* Mask for the number of system modes */
#define WND_SYSMODE_MASK             0x3

/* Mask for override control word */
#define WND_OVERRIDE_MODE_MASK       (0xFFFF ^ WIND_NOISE_DETECT_CONTROL_MODE_OVERRIDE)

#define WND_MIN_VALID_SINKS          ((1 << WND_1MIC_TERMINAL))
#define WND_MIN_VALID_SOURCES        0

/* Terminals to skip data propagation in base class */
#define WND_TERMINAL_SKIP_MASK       0

#define WND_SUPPORTS_IN_PLACE        TRUE
#define WND_SUPPORTS_METADATA        TRUE
#define WND_DYNAMIC_BUFFERS_FALSE    FALSE

/* Capability minor version */
#define WND_CAP_VERSION_MINOR        0

#ifdef RUNNING_ON_KALSIM
#define WND_KALSIM_FLAG_MASK         0xF
/* Second most significant nibble in AANC kalsim FLAGS is reserved for WND */
#define WND_KALSIM_FLAGS_SHIFT       24
#endif /* RUNNING_ON_KALSIM */

#define WND_MEM_TABLE_SIZE           6

/******************************************************************************
Public Enums
*/

typedef enum
{
    WND_UNUSED_TERMINAL, /* 0 */
    WND_1MIC_TERMINAL,   /* 1 */
    WND_2MIC_TERMINAL,   /* 2 */
    WND_MAX_TERMINALS    /* 3 */
} WND_TERMINALS;

typedef enum
{
    EN_WND_INTENSITY_NONE,
    EN_WND_INTENSITY_LOW,
    EN_WND_INTENSITY_MEDIUM,
    EN_WND_INTENSITY_HIGH
} WND_INTENSITY;

/******************************************************************************
Public Type Declarations
*/

/* WND operator data */
typedef struct wnd_exop
{
    unsigned sample_rate;               /* Operator sample rate */
    CAP_ID cap_id;                      /* Capability ID */
    WND_PARAMETERS wnd_cap_params;      /* Operator parameters */

    unsigned cur_mode;                  /* Current mode */
    unsigned ovr_control;               /* Override status */
    unsigned host_mode;                 /* Mode setting from apps/host */
    unsigned qact_mode;                 /* Mode setting from QACT */

    unsigned detect;                    /* Wind detection flags */
    WND_INTENSITY intensity;            /* Wind intensity */
    int power;                          /* External mic power (dBFS, Q12.N) */

    WND_EVENT detect_event;             /* Wind detection event state machine */

    void *f_handle;                     /* Feature handle for licensing */

    tCbuffer *p_ext_mic;                /* External mic buffer pointer */
    tCbuffer *p_div_mic;                /* Diversity mic buffer pointer */

    malloc_t_entry *p_table;            /* Pointer to memory allocation table */
    WND200_1MIC_DMX *p_wnd200_1mic;     /* Pointer to wnd200 1-mic module */
    uint8 *p_wnd200_1mic_dm1;
    WND200_2MIC_DMX *p_wnd200_2mic;     /* Pointer to wnd200 2-mic module */
    uint8 *p_wnd200_2mic_dm1;
    WND200_COMMON_DMX *p_wnd200_common; /* Pointer to wnd200 common module */
    uint8 *p_wnd200_common_dm1;
    tCbuffer *p_tmp_wnd;                /* Temporary buffer for WND processing */
} WND_OP_DATA;

/******************************************************************************
Private Function Definitions
*/

/* Standard Capability API handlers */
extern bool wnd_create(OPERATOR_DATA *op_data, void *message_data,
                       unsigned *response_id, void **resp_data);
extern bool wnd_destroy(OPERATOR_DATA *op_data, void *message_data,
                        unsigned *response_id, void **resp_data);

/* Standard Opmsg handlers */
extern bool wnd_opmsg_set_control(OPERATOR_DATA *op_data,
                                  void *message_data,
                                  unsigned *resp_length,
                                  OP_OPMSG_RSP_PAYLOAD **resp_data);
extern bool wnd_opmsg_get_status(OPERATOR_DATA *op_data,
                                 void *message_data,
                                 unsigned *resp_length,
                                 OP_OPMSG_RSP_PAYLOAD **resp_data);

/* Custom opmsg handlers*/
/**
 * \brief  Retrieve the power and intensity values
 *
 * \param  op_data          Pointer to operator data
 * \param  message_data     Pointer to message data
 * \param  resp_length      Length of response
 * \param  resp_data        Pointer to response data
 *
 * \return  boolean indicating success or failure.
 *
 */
extern bool wnd_opmsg_get_power_intensity(OPERATOR_DATA *op_data,
                                          void *message_data,
                                          unsigned *resp_length,
                                          OP_OPMSG_RSP_PAYLOAD **resp_data);

/* Standard data processing function */
extern void wnd_process_data(OPERATOR_DATA *op_data,
                             TOUCHED_TERMINALS *touched);

/* Capability hook functions to work with base_aud_cur_op */
bool wnd_connect_hook(OPERATOR_DATA *op_data, unsigned terminal_id);
bool wnd_disconnect_hook(OPERATOR_DATA *op_data, unsigned terminal_id);

#endif /* _WIND_NOISE_DETECT_H_ */
